<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class dept extends admin {
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('dept_model');
	}
	
	function init () {
		$tree = pc_base::load_sys_class('tree');
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$userid = $_SESSION['userid'];
		$admin_username = param::get_cookie('admin_username');
		
		$table_name = $this->db->table_name;
	
		$result = $this->db->select('','*','','listorder ASC,id ASC');
		//管理人员名称
		$admin_names = pc_base::load_model('admin_model')->rnames();
		
		$array = array();
		foreach($result as $r) {
			$r['cname'] = ($r['name']);
			$r['manager'] = '-';
			if(!$r['child'])
			$r['manager'] = $r['mid']?(($r['mmemo']?'('.$r['mmemo'].')':'').$admin_names[$r['mid']].($r['mstatus']?'(兼)':'')):'暂缺';
			$r['str_manage'] = '<a href="?m=admin&c=dept&a=adm&id='.$r['id'].'">经理</a> | <a href="?m=admin&c=dept&a=add&parentid='.$r['id'].'&menuid='.$_GET['menuid'].'">添加下级部门</a> | <a href="?m=admin&c=dept&a=edit&id='.$r['id'].'&menuid='.$_GET['menuid'].'">'.L('modify').'</a> | <a href="javascript:confirmurl(\'?m=admin&c=dept&a=delete&id='.$r['id'].'&menuid='.$_GET['menuid'].'\',\''.L('confirm',array('message'=>$r['cname'])).'\')">'.L('delete').'</a> ';
			$array[] = $r;
		}

		$str  = "<tr>
					<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
					<td align='center'>\$id</td>
					<td >\$spacer\$cname</td>
					<td >\$manager</td>
					<td align='center'>\$str_manage</td>
				</tr>";
		$tree->init($array);
		$categorys = $tree->get_tree(0, $str);
		include $this->admin_tpl('dept');
	}
	
	function adm() {
		if(isset($_POST['dosubmit'])) {
			$id = $_POST['id'];
			$this->db->update($_POST['info'],array('id'=>$id));
			
			showmessage(L('add_success'),'?m=admin&c=dept');
		} else {
			$id = $_GET['id'];
			//管理人员名称
			$admin_names = pc_base::load_model('admin_model')->rnames();;
			include $this->admin_tpl('dept');
		}
	}
	function add() {
		if(isset($_POST['dosubmit'])) {
			$this->db->insert($_POST['info']);
			//开发过程中用于自动创建语言包
			
			//结束
			showmessage(L('add_success'));
		} else {
			$show_validator = '';
			$tree = pc_base::load_sys_class('tree');
			$result = $this->db->select();
			
			$array = array();
			foreach($result as $r) {
				$r['cname'] = ($r['name']);
				$r['selected'] = $r['id'] == $_GET['parentid'] ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			
			include $this->admin_tpl('dept');
		}
	}
	function delete() {
		$_GET['id'] = intval($_GET['id']);
		$this->db->delete(array('id'=>$_GET['id']));
		showmessage(L('operation_success'));
	}
	
	function edit() {
		if(isset($_POST['dosubmit'])) {
			$id = intval($_POST['id']);
			//print_r($_POST['info']);exit;
			$r = $this->db->get_one(array('id'=>$id));
			$this->db->update($_POST['info'],array('id'=>$id));
			
			//结束语言文件修改
			showmessage(L('operation_success'));
		} else {
			$show_validator = $array = $r = '';
			$tree = pc_base::load_sys_class('tree');
			$id = intval($_GET['id']);
			$r = $this->db->get_one(array('id'=>$id));
			if($r) extract($r);
			$result = $this->db->select();
			foreach($result as $r) {
				$r['cname'] = ($r['name']);
				$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$cname</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			include $this->admin_tpl('dept');
		}
	}
	
	/**
	 * 排序
	 */
	function listorder() {
		if(isset($_POST['dosubmit'])) {
			foreach($_POST['listorders'] as $id => $listorder) {
				$this->db->update(array('listorder'=>$listorder),array('id'=>$id));
			}
			showmessage(L('operation_success'));
		} else {
			showmessage(L('operation_failure'));
		}
	}
	
}
?>